﻿using Wpf.Ui;

namespace Digitalmes.Gallery.Services;

/// <summary>
/// Managed host of the application.
/// </summary>
public class ApplicationHostService : IHostedService
{
    private readonly IServiceProvider _serviceProvider;

    private INavigationWindow? _navigationWindow;

    public ApplicationHostService(IServiceProvider serviceProvider)
    {
        _serviceProvider = serviceProvider;
    }

    /// <summary>
    /// Triggered when the application host is ready to start the service.
    /// </summary>
    /// <param name="cancellationToken">Indicates that the start process has been aborted.</param>
    public async Task StartAsync(CancellationToken cancellationToken)
    {
        await HandleActivationAsync();
    }

    /// <summary>
    /// Triggered when the application host is performing a graceful shutdown.
    /// </summary>
    /// <param name="cancellationToken">Indicates that the shutdown process should no longer be graceful.</param>
    public async Task StopAsync(CancellationToken cancellationToken)
    {
        await Task.CompletedTask;
    }

    /// <summary>
    /// Creates main window during activation.
    /// </summary>
    private async Task HandleActivationAsync()
    {
        if (!Application.Current.Windows.OfType<MainWindow>().Any())
        {
            _navigationWindow = _serviceProvider.GetRequiredService<INavigationWindow>();
            _navigationWindow.ShowWindow();

            // 根据菜单显示设定的导航页面。
            var navViewService = _serviceProvider.GetRequiredService<INavigationViewService>();
            var navView = navViewService.GetNavigationViews().FirstOrDefault();
            _navigationWindow.Navigate(navView != null ? navView.TargetPageType! : typeof(DashboardPage));
        }

        await Task.CompletedTask;
    }
}
